# -*- coding: utf-8 -*-
'''
@author: masaikk
协同过滤
'''

import pandas as pd
import numpy as np
from pandas.tests.reshape.merge.test_merge_index_as_string import df2


class Xietonggl():
    def __init__(self, df, use_num):
        self.df = df
        self.use_num = use_num

    def guanliandu(self, ):
        df_array = np.array(self.df)  # 将数据矩阵化
        df_array_t = df_array.T  # 矩阵转置
        user_guanxi = np.dot(df_array, df_array_t)  # 用户之间的关联度矩阵
        prod_guanxi = np.dot(df_array_t, df_array)  # 产品之间的关联度矩阵

        df_user = pd.DataFrame(user_guanxi, index=self.df.index, columns=self.df.index)  # 用户数据框
        df_prod = pd.DataFrame(prod_guanxi, index=self.df.columns, columns=self.df.columns)  # 产品数据框
        return df_user, df_prod

    def oneself(self, ):
        pro = pd.DataFrame(self.df.loc[self.use_num])
        user_old = list(pro[pro[self.use_num] == 1].index)  # 用户自身使用的产品
        return pro, user_old

    def yonghu(self, ):
        # 找出关系最近的5个用户
        df_user, df_prod = self.guanliandu()
        pro, user_old = self.oneself()
        df_tmp = pd.DataFrame(df_user[self.use_num].sort_values(ascending=False))
        user_jin = list(df_tmp.iloc[1:6, :].index)
        jin_5 = self.df.loc[user_jin]
        user_no = list(pro[pro[self.use_num] == -1].index)
        user_tt = jin_5[user_no].sum()
        '''
        还可以排序下再取
        '''
        user_zong = list(user_tt[user_tt.values != -5].index)
        return user_zong

    def prod(self, ):
        # 找出每个产品关系最近的一个产品
        df_user, df_prod = self.guanliandu()
        pro, user_old = self.oneself()
        pro_zong = []
        for i in user_old:
            df_p = pd.DataFrame(df_prod[i].sort_values(ascending=False))
            pro_1 = list(df_p.iloc[1:2, :].index)[0]
            pro_zong.append(pro_1)
        return pro_zong

    def tuijian(self, ):
        user_zong = self.yonghu()
        pro_zong = self.prod()
        tj = user_zong + pro_zong
        item = set(tj)
        ttjj = {}
        for j in item:
            cn = tj.count(j)
            ttjj.update({j: cn})

        df_tj = pd.DataFrame(ttjj, index=['rank']).T
        tuij = df_tj.sort_values(['rank'], ascending=False)
        return tuij


def main():
    tj = Xietonggl(df2, 20180110).tuijian()
    # bug
    print(tj)


if __name__ == '__main__':
    main()